/*
 * Copyright the original author or authors.
 * 
 * Licensed under the MOZILLA PUBLIC LICENSE, Version 1.1 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.mozilla.org/MPL/MPL-1.1.html
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
 
package com.airlogger.log.model
{
	import com.bourre.plugin.Plugin;
	import com.bourre.model.AbstractModel;
	import com.bourre.events.BasicEvent;
	import com.bourre.events.StringEvent;
	import com.airlogger.log.event.LogDataEvent;
	import com.bourre.log.PixlibDebug;	
	
	public class UIModel extends AbstractModel
	{
		public static const onFocusChangedEVENT : String = "onFocusChanged";
		public static const onRefreshEVENT : String = "onRefresh";
		public static const onLogAddedEVENT : String = "onLogAdded";
		public static const onSWFAddedEVENT : String = "onSWFAdded";
		public static const onSWFRemovedEVENT : String = "onSWFRemoved";		public static const onTabChangedEVENT : String = "onTabChanged";
		
		[ArrayElementType("SWFData")] 
		protected var swfs : Array;
			
		protected var currentSession : String;	
		protected var tabsNameCount : Object;		protected var tabsName : Object;
				
		public function UIModel( owner : Plugin = null, name : String = null )
		{
			super(owner, name);
			currentSession = "";
			swfs = new Array();
			tabsName = new Object();
			tabsNameCount = new Object();
		}

		public function setFocus ( id : String ) : void
		{
			currentSession = id;
			
			fireOnRefreshEvent();
			
			//getLogger().debug( "fire focus for " + id );
			
			fireOnFocusChangedEvent();
		}
		public function getFocus () : String
		{
			return currentSession;
		}
		
		public function addSWF ( id : String, name : String = null ) : void
		{
			var n : String;
			if( name )
			{
				if( isNameRegistered( name ) ) 
				{
					n = name + getNameIncrement( name );
				}
				else
				{
					n = name;
					registerName( name );
				}
				registerNameWithId( id, name );
			}
			else
			{
				n = "SWF #" + id;
			}
			
			swfs.push(new SWFData( id, n ) );
		}		
		public function removeSWF ( id : String ) : void
		{
			var pos : Number;
			loop : for each( var data : SWFData in swfs )
			{
				if( data.id == id )
				{
					pos = swfs.indexOf( data );
					swfs.splice( pos, 1 );
					break loop;
				}
			} 
		}
		public function getSWF ( id : String ) : SWFData 
		{
			for each( var data : SWFData in swfs )
			{
				if( data.id == id )
				{
					return data;
				}
			}
			return null;
		}
		public function getSWFByName ( name : String ) : SWFData
		{
			for each( var data : SWFData in swfs )
			{
				if( data.name == name )
				{
					return data;
				}
			}
			return null;
		}
		public function getSWFs () : Array
		{
			return swfs;
		}
		
		public function registerName ( name : String ) : void
		{
			tabsNameCount[ name ] = 1;
		}
		public function registerNameWithId ( id :String,  name : String ) : void
		{
			tabsName[ id ] = name;
		}
		public function isRegistered ( id : String ) : Boolean
		{
			return tabsName[ id ] != null;
		}
		public function isNameRegistered ( name : String ) : Boolean
		{
			return tabsNameCount[ name ] != null;
		}
		public function getRegistredName ( id : String ) : String 
		{
			return tabsName[ id ];
		}
		public function getNameIncrement ( name : String ) : String
		{
			return " (" + ( tabsNameCount[ name ] += 1 ) + ")";
		}

		public function fireOnSWFAddedEvent ( id : String ) : void
		{
			notifyChanged( new StringEvent ( onSWFAddedEVENT, this, id ) );
		}
		public function fireOnSWFRemovedEvent ( id : String ) : void
		{
			
			notifyChanged( new StringEvent ( onSWFRemovedEVENT, this, id ) );
		}
		public function fireOnFocusChangedEvent () : void
		{
			notifyChanged( new StringEvent ( onFocusChangedEVENT, this, currentSession ) );
		}
		public function fireOnRefreshEvent () : void
		{
			notifyChanged( new StringEvent ( onRefreshEVENT, this, currentSession ) );
		}
		public function fireOnTabChanged () : void
		{
			notifyChanged(new BasicEvent(onTabChangedEVENT, this ) );
		}
		public function fireOnLogAddedEvent( data : LogData ) : void
		{
			notifyChanged( new LogDataEvent ( onLogAddedEVENT, this, data ) );
		}
	}
}